package com.hyts.bmsp.web.account;


import com.hyts.bmsp.model.account.AccountInfoBean;
import com.hyts.bmsp.model.account.ChangePassWordDTO;
import com.hyts.bmsp.model.account.LoadUserInfoDTO;
import com.hyts.bmsp.model.account.UserInfoBean;
import com.hyts.bmsp.service.account.*;
import com.hyts.bmsp.bean.ResultJsonBean;
import com.hyts.bmsp.controller.BaseController;
import com.hyts.bmsp.controller.WebController;
import com.hyts.bmsp.session.WebSessionManager;
import com.jfinal.log.Log;

/**
 * @ClassName: UserInfoController
 * @Description: 用户信息操作控制器
 * @author: 李博/Alex
 * @date: 2017-2-15 下午07:41:21
 */
public class UserInfoController extends BaseController {

	
	private UserInfoService service = new UserInfoServiceImpl();

	
	/**
	 * @fieldName: log
	 * @fieldType: Log
	 * @Description: 日志输出器
	 */
	private static final Log log = Log.getLog(UserInfoController.class);
	
	/**
	 * @fieldName: JSP_BASE_PATH
	 * @fieldType: String
	 * @Description: jsp页面基础路径
	 */
	private static final String JSP_MAIN_PATH = "/admin/account/account_index";
	
	/**
	 * @Title: index
	 * @Description: 跳转到登录页面
	 * @return: void
	 */
	public void index()
	{
		log.debug("跳转到系统主页面");
		this.toJsp(JSP_MAIN_PATH);
	}
	
	public void changePassword()
    {
	    log.debug("跳转到更新密码页面");
	    toJsp("/admin/account/change_password");
    }
	
	
	/**
	 * @Title: loadData2ChangePassword
	 * @Description: 加载数据根据更新密码
	 * @return: void
	 */
	public void loadData2ChangePassword(){
		String userId = (String) WebSessionManager.getAttribute(getRequest(),WebSessionManager.SESSION_VALUE_TOKEN_ID);
		AccountInfoBean bean = service.loadAccountInfo(userId);
	    ResultJsonBean result = new ResultJsonBean(true, "查询成功");
		ChangePassWordDTO obj = new ChangePassWordDTO();
		obj.accountName	= bean.get("ACCOUNT_NAME");
		result.setEntity(obj);
		toJson(result);
	}
	
	/**
	 * @Title: doChangePassword
	 * @Description: 执行更新密码操作
	 * @return: void
	 */
	public void doChangePassword(){
		String userId = (String) WebSessionManager.getAttribute(getRequest(),WebSessionManager.SESSION_VALUE_TOKEN_ID);
	    ResultJsonBean result = new ResultJsonBean(true, "密码更新成功");
		AccountInfoBean bean = service.loadAccountInfo(userId);
		if(!bean.get("PASSWORD").equals(this.getParameter("oldPassword")))
		{
			result = new ResultJsonBean(false, "旧密码错误!");
		    toJson(result);
		    return ;
		}
		ChangePassWordDTO dto = new ChangePassWordDTO();
		dto.newPassword = this.getParameter("newPassword");
		dto.userId = userId;
		//更新密码
		service.updateAccountInfo(dto);
		toJson(result);
	}
	
	
	/**
	 * @Title: loadUserInfoData
	 * @Description: 加载用户信息数据
	 * @return: void
	 */
	public void loadUserInfoData(){
		String userId = (String) WebSessionManager.getAttribute(getRequest(),WebSessionManager.SESSION_VALUE_TOKEN_ID);
		ResultJsonBean result = new ResultJsonBean(true, "查询成功");
		UserInfoBean bean = service.loadUserInfo(userId);
		LoadUserInfoDTO dto = new LoadUserInfoDTO();
		dto.setAliasName(bean.getStr("ALIAS_NAME"));
		dto.setRealName(bean.getStr("REAL_NAME"));
		dto.setEmail(bean.getStr("EMAIL"));
		//dto.setId(bean.getStr("id"));
		dto.setQq(bean.getStr("QQ"));
		dto.setRemark(bean.getStr("REMARK"));
		dto.setTelephone(bean.getStr("TELEPHONE"));
		dto.setWeiChat(bean.getStr("WEI_CHAT"));
		result.setEntity(dto);
		toJson(result);
	}
	
	/**
	 * @Title: doChangeUserInfoData
	 * @Description: 执行更新个人信息数据
	 * @return: void
	 */
	public void doChangeUserInfoData(){
		ResultJsonBean result = new ResultJsonBean(true, "更新个人信息成功");
		LoadUserInfoDTO dto = new LoadUserInfoDTO();
		String userId = (String) WebSessionManager.getAttribute(getRequest(),WebSessionManager.SESSION_VALUE_TOKEN_ID);
		try {
			this.injectRequestParamToObj(dto);
			boolean flag = service.updateUserInfo(userId, dto);
			System.out.println(dto);
			if(!flag){
				result = new ResultJsonBean(false, "更新个人信息失败");
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		toJson(result);
	}
	
	public void toOAuth()
    {
	    log.debug("跳转到第三方授权主页面");
	    toJsp("/admin/account/add");
    }
}
